using System;
using System.Linq;
using System.Collections.Generic;
using DevExpress.CodeRush.StructuralParser;
using CR.StyleCop.Utils;

namespace CR.StyleCop.Maintainability {
    public class FileMayOnlyContainASingleNamespace : BaseStyleCopIssueSearcher<INamespaceElement> {

        public override IEnumerable<StyleCopIssue> FindIssues(IEnumerable<INamespaceElement> elements) {
            if (elements.Count() <= 1)
                return Utils.SourceUtils.EmptyIssueList;

            // NOTE (fen): If a namespace is declared like follows namespace A.B.C, B and C
            // is treated as INamespaceElement this is probably a bug. A work around to fix this is
            // to check if there is any LanguageElement for the INamespaceElement I get, if the
            // return value is null its a fake INamespaceElement and I can ignore it.
            return elements.Skip(1).Where(e => SourceUtils.ConvertToLanguageElement(e) != null)
                .Select(e => new StyleCopIssue { 
                Element = e, 
                IssueDescription = Resource.FileMayOnlyContainASingleNamespaceIssueDescription
            });
        }
    }
}
